package map;

public class ComparableDemo implements Comparable{

	
	private static Object o;
	/**
	 * Comparable interface is used to order the object of user defined classes.
	 * Interface is found in the java.lang package and it has only one method named campareTo(Object)
	 * it provide only single sotring sequence i.e. you can store the element in single data member 
	 * For Instance it may be either - name,age,rollno etc
	 */
	/*
	 * Syntax:

        public int compareTo(Object obj): is used to compare the current object with the specified object.
        
        We can sort the elements of:

        1-String objects
        2-Wrapper class objects
        3-User-defined class objects
        
       #### Collections class provides static methods for sorting the elements of collection.
            If collection elements are of Set type, we can use TreeSet.But We cannot sort the elements
            of List.Collections class provides methods for sorting the elements of List type elements.

	 */
	/**
	 * Method of Collections class for sorting List elements
	 * public void sort(List list): is used to sort the elements of List.List elements must be of-
	 *  Comparable type.
	 * Note: String class and Wrapper classes implements the Comparable interface.So if you store-
	 *  the objects of string or wrapper classes, it will be Comparable.
	 */
	int rollno;
	String name;
	int age;
	public ComparableDemo(int rollno, String name, int age ) {
	this.rollno = rollno;
	this.name= name;
    this.age = age;
	}
	@Override
	public int compareTo(Object o) {
	
		ComparableDemo cd=(ComparableDemo)o;
		if (age==cd.age)
		return 0;
		else if(age>cd.age)
		return 1;
		else
			return-1;
			
	}
	@Override
	public String toString() {
		return "ComparableDemo [rollno=" + rollno + ", name=" + name + ", age="
				+ age + "]";
	}
	
}
